package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.PaymentService;
import cc.rengu.igas.momp.facade.request.*;
import cc.rengu.igas.momp.facade.response.*;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;

/**
 * 支付接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/24 11:56
 */
@SuppressWarnings("unchecked")
public class PaymentServiceImpl implements PaymentService {

    /**
     * (合并)支付接口Dubbo实现
     *
     * @param comboPaymentRequest (合并)支付接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.ComboPaymentResponse> (合并)支付接口应答对象
     * @author Jinan Liu
     * @date 2020/6/3 14:31
     */
    @Override
    public Result<ComboPaymentResponse> comboPayment(ComboPaymentRequest comboPaymentRequest) {

        DubboService<ComboPaymentRequest> dubboService = new DubboServiceImpl();
        Result<ComboPaymentResponse> comboPaymentResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.COMBO_PAYMENT.getTxnNum().equals(comboPaymentRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ComboPaymentResponse> dubboResult = dubboService.callAtomService(comboPaymentRequest,
                    MompTxnNumEnum.COMBO_PAYMENT.getService(), ComboPaymentResponse.class);
            comboPaymentResponseResult.setResult(dubboResult.getResult());
            comboPaymentResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ComboPaymentResponse comboPaymentResponse = new ComboPaymentResponse();
            comboPaymentResponseResult.setResult(comboPaymentResponse);
            comboPaymentResponse.setHeader(comboPaymentRequest.getHeader());
            comboPaymentResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                comboPaymentResponse.setRespCode(((BizException) e).getCode());
                comboPaymentResponse.setRespDesc(e.getMessage());
            } else {
                comboPaymentResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                comboPaymentResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return comboPaymentResponseResult;
    }

    /**
     * (合并)支付冲正接口Dubbo实现
     *
     * @param comboPaymentReversalRequest (合并)支付冲正接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.ComboPaymentReversalResponse> (合并)支付冲正接口应答对象
     * @author Jinan Liu
     * @date 2020/6/3 14:31
     */
    @Override
    public Result<ComboPaymentReversalResponse> comboPaymentReversal(ComboPaymentReversalRequest comboPaymentReversalRequest) {

        DubboService<ComboPaymentReversalRequest> dubboService = new DubboServiceImpl();
        Result<ComboPaymentReversalResponse> comboPaymentReversalResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.COMBO_PAYMENT_REVERSAL.getTxnNum().equals(comboPaymentReversalRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ComboPaymentReversalResponse> dubboResult = dubboService.callAtomService(comboPaymentReversalRequest,
                    MompTxnNumEnum.COMBO_PAYMENT_REVERSAL.getService(), ComboPaymentReversalResponse.class);
            comboPaymentReversalResponseResult.setResult(dubboResult.getResult());
            comboPaymentReversalResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ComboPaymentReversalResponse comboPaymentReversalResponse = new ComboPaymentReversalResponse();
            comboPaymentReversalResponseResult.setResult(comboPaymentReversalResponse);
            comboPaymentReversalResponse.setHeader(comboPaymentReversalRequest.getHeader());
            comboPaymentReversalResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                comboPaymentReversalResponse.setRespCode(((BizException) e).getCode());
                comboPaymentReversalResponse.setRespDesc(e.getMessage());
            } else {
                comboPaymentReversalResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                comboPaymentReversalResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return comboPaymentReversalResponseResult;
    }

    /**
     * (合并)支付退货接口Dubbo实现
     *
     * @param comboPaymentRefundRequest (合并)支付退货接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.ComboPaymentRefundResponse> (合并)支付退货接口应答对象
     * @author Jinan Liu
     * @date 2020/6/3 14:31
     */
    @Override
    public Result<ComboPaymentRefundResponse> comboPaymentRefund(ComboPaymentRefundRequest comboPaymentRefundRequest) {

        DubboService<ComboPaymentRefundRequest> dubboService = new DubboServiceImpl();
        Result<ComboPaymentRefundResponse> comboPaymentRefundResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.COMBO_PAYMENT_REFUND.getTxnNum().equals(comboPaymentRefundRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ComboPaymentRefundResponse> dubboResult = dubboService.callAtomService(comboPaymentRefundRequest,
                    MompTxnNumEnum.COMBO_PAYMENT_REFUND.getService(), ComboPaymentRefundResponse.class);
            comboPaymentRefundResponseResult.setResult(dubboResult.getResult());
            comboPaymentRefundResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ComboPaymentRefundResponse comboPaymentRefundResponse = new ComboPaymentRefundResponse();
            comboPaymentRefundResponseResult.setResult(comboPaymentRefundResponse);
            comboPaymentRefundResponse.setHeader(comboPaymentRefundRequest.getHeader());
            comboPaymentRefundResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                comboPaymentRefundResponse.setRespCode(((BizException) e).getCode());
                comboPaymentRefundResponse.setRespDesc(e.getMessage());
            } else {
                comboPaymentRefundResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                comboPaymentRefundResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return comboPaymentRefundResponseResult;
    }

    /**
     * 手续费优惠接口Dubbo实现
     *
     * @param comboMchntFeeDiscountRequest 手续费优惠接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.ComboMchntFeeDiscountResponse> 手续费优惠接口应答对象
     * @author Jinan Liu
     * @date 2020/6/3 14:31
     */
    @Override
    public Result<ComboMchntFeeDiscountResponse> comboMchntFeeDiscount(ComboMchntFeeDiscountRequest comboMchntFeeDiscountRequest) {

        DubboService<ComboMchntFeeDiscountRequest> dubboService = new DubboServiceImpl();
        Result<ComboMchntFeeDiscountResponse> comboMchntFeeDiscountResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.FEE_DISCOUNT.getTxnNum().equals(comboMchntFeeDiscountRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ComboMchntFeeDiscountResponse> dubboResult = dubboService.callAtomService(comboMchntFeeDiscountRequest,
                    MompTxnNumEnum.FEE_DISCOUNT.getService(), ComboMchntFeeDiscountResponse.class);
            comboMchntFeeDiscountResponseResult.setResult(dubboResult.getResult());
            comboMchntFeeDiscountResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ComboMchntFeeDiscountResponse comboMchntFeeDiscountResponse = new ComboMchntFeeDiscountResponse();
            comboMchntFeeDiscountResponseResult.setResult(comboMchntFeeDiscountResponse);
            comboMchntFeeDiscountResponse.setHeader(comboMchntFeeDiscountRequest.getHeader());
            comboMchntFeeDiscountResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                comboMchntFeeDiscountResponse.setRespCode(((BizException) e).getCode());
                comboMchntFeeDiscountResponse.setRespDesc(e.getMessage());
            } else {
                comboMchntFeeDiscountResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                comboMchntFeeDiscountResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return comboMchntFeeDiscountResponseResult;
    }

    /**
     * 手续费优惠冲正接口Dubbo实现
     *
     * @param comboMchntFeeDiscountReversalRequest 手续费优惠冲正接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.ComboMchntFeeDiscountReversalResponse> 手续费优惠冲正接口应答对象
     * @author Jinan Liu
     * @date 2020/6/3 14:31
     */
    @Override
    public Result<ComboMchntFeeDiscountReversalResponse> comboMchntFeeDiscountReversal(ComboMchntFeeDiscountReversalRequest comboMchntFeeDiscountReversalRequest) {

        DubboService<ComboMchntFeeDiscountReversalRequest> dubboService = new DubboServiceImpl();
        Result<ComboMchntFeeDiscountReversalResponse> comboMchntFeeDiscountReversalResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.FEE_DISCOUNT_REVERSAL.getTxnNum().equals(comboMchntFeeDiscountReversalRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ComboMchntFeeDiscountReversalResponse> dubboResult = dubboService.callAtomService(comboMchntFeeDiscountReversalRequest,
                    MompTxnNumEnum.FEE_DISCOUNT_REVERSAL.getService(), ComboMchntFeeDiscountReversalResponse.class);
            comboMchntFeeDiscountReversalResponseResult.setResult(dubboResult.getResult());
            comboMchntFeeDiscountReversalResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ComboMchntFeeDiscountReversalResponse comboMchntFeeDiscountReversalResponse = new ComboMchntFeeDiscountReversalResponse();
            comboMchntFeeDiscountReversalResponseResult.setResult(comboMchntFeeDiscountReversalResponse);
            comboMchntFeeDiscountReversalResponse.setHeader(comboMchntFeeDiscountReversalRequest.getHeader());
            comboMchntFeeDiscountReversalResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                comboMchntFeeDiscountReversalResponse.setRespCode(((BizException) e).getCode());
                comboMchntFeeDiscountReversalResponse.setRespDesc(e.getMessage());
            } else {
                comboMchntFeeDiscountReversalResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                comboMchntFeeDiscountReversalResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return comboMchntFeeDiscountReversalResponseResult;
    }

    /**
     * 手续费优惠回退接口Dubbo实现
     *
     * @param comboMchntFeeDiscountRefundRequest 手续费优惠回退接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.ComboMchntFeeDiscountRefundResponse> 手续费优惠回退接口应答对象
     * @author Jinan Liu
     * @date 2020/6/3 14:31
     */
    @Override
    public Result<ComboMchntFeeDiscountRefundResponse> comboMchntFeeDiscountRefund(ComboMchntFeeDiscountRefundRequest comboMchntFeeDiscountRefundRequest) {

        DubboService<ComboMchntFeeDiscountRefundRequest> dubboService = new DubboServiceImpl();
        Result<ComboMchntFeeDiscountRefundResponse> comboMchntFeeDiscountRefundResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.FEE_DISCOUNT_REFUND.getTxnNum().equals(comboMchntFeeDiscountRefundRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ComboMchntFeeDiscountRefundResponse> dubboResult = dubboService.callAtomService(comboMchntFeeDiscountRefundRequest,
                    MompTxnNumEnum.FEE_DISCOUNT_REFUND.getService(), ComboMchntFeeDiscountRefundResponse.class);
            comboMchntFeeDiscountRefundResponseResult.setResult(dubboResult.getResult());
            comboMchntFeeDiscountRefundResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ComboMchntFeeDiscountRefundResponse comboMchntFeeDiscountRefundResponse = new ComboMchntFeeDiscountRefundResponse();
            comboMchntFeeDiscountRefundResponseResult.setResult(comboMchntFeeDiscountRefundResponse);
            comboMchntFeeDiscountRefundResponse.setHeader(comboMchntFeeDiscountRefundRequest.getHeader());
            comboMchntFeeDiscountRefundResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                comboMchntFeeDiscountRefundResponse.setRespCode(((BizException) e).getCode());
                comboMchntFeeDiscountRefundResponse.setRespDesc(e.getMessage());
            } else {
                comboMchntFeeDiscountRefundResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                comboMchntFeeDiscountRefundResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return comboMchntFeeDiscountRefundResponseResult;
    }

    /**
     * 手续费优惠回退试算接口Dubbo实现
     *
     * @param comboMchntFeeDiscountTrialRefundRequest 手续费优惠回退试算接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.ComboMchntFeeDiscountTrialRefundResponse> 手续费优惠回退试算接口应答对象
     * @author Jinan Liu
     * @date 2020/6/3 14:31
     */
    @Override
    public Result<ComboMchntFeeDiscountTrialRefundResponse> comboMchntFeeDiscountTrialRefund(ComboMchntFeeDiscountTrialRefundRequest comboMchntFeeDiscountTrialRefundRequest) {

        DubboService<ComboMchntFeeDiscountTrialRefundRequest> dubboService = new DubboServiceImpl();
        Result<ComboMchntFeeDiscountTrialRefundResponse> comboMchntFeeDiscountTrialRefundResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.TRIAL_FEE_DISCOUNT_REFUND.getTxnNum().equals(comboMchntFeeDiscountTrialRefundRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ComboMchntFeeDiscountTrialRefundResponse> dubboResult = dubboService.callAtomService(comboMchntFeeDiscountTrialRefundRequest,
                    MompTxnNumEnum.TRIAL_FEE_DISCOUNT_REFUND.getService(), ComboMchntFeeDiscountTrialRefundResponse.class);
            comboMchntFeeDiscountTrialRefundResponseResult.setResult(dubboResult.getResult());
            comboMchntFeeDiscountTrialRefundResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ComboMchntFeeDiscountTrialRefundResponse comboMchntFeeDiscountTrialRefundResponse = new ComboMchntFeeDiscountTrialRefundResponse();
            comboMchntFeeDiscountTrialRefundResponseResult.setResult(comboMchntFeeDiscountTrialRefundResponse);
            comboMchntFeeDiscountTrialRefundResponse.setHeader(comboMchntFeeDiscountTrialRefundRequest.getHeader());
            comboMchntFeeDiscountTrialRefundResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                comboMchntFeeDiscountTrialRefundResponse.setRespCode(((BizException) e).getCode());
                comboMchntFeeDiscountTrialRefundResponse.setRespDesc(e.getMessage());
            } else {
                comboMchntFeeDiscountTrialRefundResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                comboMchntFeeDiscountTrialRefundResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return comboMchntFeeDiscountTrialRefundResponseResult;
    }

    /**
     * 交易调账接口DUBBO实现
     *
     * @param txnDataRegulationRequest 交易调账接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.TxnDataRegulationResponse> 交易调账接口应答对象
     * @author Jinan Liu
     * @date 2020/6/14 10:44
     */
    @Override
    public Result<TxnDataRegulationResponse> txnDataRegulation(TxnDataRegulationRequest txnDataRegulationRequest) {

        DubboService<TxnDataRegulationRequest> dubboService = new DubboServiceImpl();
        Result<TxnDataRegulationResponse> txnDataRegulationResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.TXN_DATA_REGULATION.getTxnNum().equals(txnDataRegulationRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<TxnDataRegulationResponse> dubboResult = dubboService.callAtomService(txnDataRegulationRequest,
                    MompTxnNumEnum.TXN_DATA_REGULATION.getService(), TxnDataRegulationResponse.class);
            txnDataRegulationResponseResult.setResult(dubboResult.getResult());
            txnDataRegulationResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            TxnDataRegulationResponse txnDataRegulationResponse = new TxnDataRegulationResponse();
            txnDataRegulationResponseResult.setResult(txnDataRegulationResponse);
            txnDataRegulationResponse.setHeader(txnDataRegulationRequest.getHeader());
            txnDataRegulationResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                txnDataRegulationResponse.setRespCode(((BizException) e).getCode());
                txnDataRegulationResponse.setRespDesc(e.getMessage());
            } else {
                txnDataRegulationResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                txnDataRegulationResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return txnDataRegulationResponseResult;
    }

    /**
     * 交易调账结果查询接口DUBBO实现
     *
     * @param queryTxnRegulationResultRequest 交易调账结果查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryTxnRegulationResultResponse> 交易调账结果查询接口应答对象
     * @author Jinan Liu
     * @date 2020/6/17 17:49
     */
    @Override
    public Result<QueryTxnRegulationResultResponse> queryTxnRegulationStatus(QueryTxnRegulationResultRequest queryTxnRegulationResultRequest) {

        DubboService<QueryTxnRegulationResultRequest> dubboService = new DubboServiceImpl();
        Result<QueryTxnRegulationResultResponse> queryTxnRegulationResultResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_TXN_REGULATION_STATUS.getTxnNum().equals(queryTxnRegulationResultRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryTxnRegulationResultResponse> dubboResult = dubboService.callAtomService(queryTxnRegulationResultRequest,
                    MompTxnNumEnum.QUERY_TXN_REGULATION_STATUS.getService(), QueryTxnRegulationResultResponse.class);
            queryTxnRegulationResultResponseResult.setResult(dubboResult.getResult());
            queryTxnRegulationResultResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryTxnRegulationResultResponse queryTxnRegulationResultResponse = new QueryTxnRegulationResultResponse();
            queryTxnRegulationResultResponseResult.setResult(queryTxnRegulationResultResponse);
            queryTxnRegulationResultResponse.setHeader(queryTxnRegulationResultRequest.getHeader());
            queryTxnRegulationResultResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryTxnRegulationResultResponse.setRespCode(((BizException) e).getCode());
                queryTxnRegulationResultResponse.setRespDesc(e.getMessage());
            } else {
                queryTxnRegulationResultResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryTxnRegulationResultResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryTxnRegulationResultResponseResult;
    }
}
